﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ToolsCollection.BLL;
using ToolsCollection.MODEL;
namespace CreateDirectory
{
	public partial class ucCreateDirectory : UserControl
	{
		private List<Directory> directorys;

		private string ConfigPath
		{
			get
			{
				if (string.IsNullOrEmpty(txtConfigPath.Text))
				{
					return "Directory.config";
				}
				else if (File.Exists(txtConfigPath.Text))
				{
					return txtConfigPath.Text;
				}
				else
				{
					return "Directory.config";
				}
			}
		}

		public ucCreateDirectory()
		{
			InitializeComponent();
		}

		private void btnGetPath_Click(object sender, EventArgs e)
		{
            
			FolderBrowserDialog dlg = new FolderBrowserDialog();
			if (dlg.ShowDialog() == DialogResult.OK)
			{
				txtDestPath.Text = dlg.SelectedPath;
			}
		}

		private void btnPreview_Click(object sender, EventArgs e)
		{
			if (!CheckValid())
			{
				return;
			}
			directorys = DirectoryHelper.GetDirectoryTreeByConfig(ConfigPath, txtTaskName.Text);
			AddTreeNode(directorys);
		}

		private void AddTreeNode(List<Directory> list)
		{
			treeDisplay.Nodes.Clear();
			foreach (Directory dir in list)
			{
				TreeNode node = new TreeNode(dir.Name);
				treeDisplay.Nodes.Add(node);
				AddTreeNode(dir.Child, node);
			}
		}

		private void AddTreeNode(List<Directory> list, TreeNode parent)
		{
			foreach (Directory dir in list)
			{
				TreeNode node = new TreeNode(dir.Name);
				parent.Nodes.Add(node);
				AddTreeNode(dir.Child, node);
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (!CheckValid())
			{
				return;
			}
			if (directorys == null)
			{
				directorys = DirectoryHelper.GetDirectoryTreeByConfig(ConfigPath, txtTaskName.Text);
			}
			if (directorys.Count > 0)
			{
				string path = Path.Combine(txtDestPath.Text, directorys[0].Path);
				if (System.IO.Directory.Exists(path))
				{
					System.IO.Directory.Delete(path, true);
				}
                
				CreateDirectory(directorys);
                ToolsCollection.MODEL.TC_BAGLIST tc_baglist = new ToolsCollection.MODEL.TC_BAGLIST { BagList_Name = this.treeDisplay.Nodes[0].Text, BagList_Path = string.Concat(this.txtDestPath.Text, "\\", this.treeDisplay.Nodes[0].Text, "\\源代码") };
                ToolsCollection.BLL.TC_BAGLIST tc_BagListBll = new ToolsCollection.BLL.TC_BAGLIST();
                tc_BagListBll.Add_TC_Bag(tc_baglist);
				MessageBox.Show("创建完成！");
			}
		}

		private void CreateDirectory(List<Directory> list)
		{
			foreach (Directory dir in list)
			{
				string path = Path.Combine(txtDestPath.Text, dir.Path);
				System.IO.Directory.CreateDirectory(path);
				CreateDirectory(dir.Child);
			}
		}
      
		private void btnClear_Click(object sender, EventArgs e)
		{
            
			Clear();
		}

		private void Clear()
		{
			txtConfigPath.Text = txtDestPath.Text = txtTaskName.Text = String.Empty;
			treeDisplay.Nodes.Clear();
		}

		private bool CheckValid()
		{
			if (string.IsNullOrEmpty(txtTaskName.Text))
			{
				MessageBox.Show("请输入任务名称！");
				return false;
			}

			if (string.IsNullOrEmpty(txtDestPath.Text))
			{
				MessageBox.Show("请选择待生成文件夹的存储路径！");
				return false;
			}

			directorys = DirectoryHelper.GetDirectoryTreeByConfig(ConfigPath, txtTaskName.Text);
			if (directorys == null)
			{
				MessageBox.Show("配置文件出错，请检查！");
			}

			return true;
		}
	}
}
